package com.tbit.uqbike.client.service.impl;

import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.client.constant.GatewayProtocol;
import com.tbit.uqbike.client.constant.RabbitMQData;
import com.tbit.uqbike.client.pojo.newEntity.PayData;
import com.tbit.uqbike.client.service.MQProducer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("mQProducerService")
public class MQProducerImpl implements MQProducer {
    private final static Logger log = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public void sendDataToQueue(String queueKey, String msg) {
        try {
            log.info(msg);
            amqpTemplate.send(queueKey, new Message(msg.getBytes("UTF-8"), new MessageProperties()));
        } catch (Exception e) {
            log.error("连接RabbitMQ异常", e);
        }
    }

    @Override
    public void sendToAi(String queueKey, String msg) {
        try {
            log.debug(msg);
            amqpTemplate.convertAndSend(queueKey, new Message(msg.getBytes("UTF-8"), new MessageProperties()));

        } catch (Exception e) {
            log.error("连接RabbitMQ异常", e);
        }
    }

    @Override
    public void sendToRide(String exchange, String queueKey, String msg) {
        try {
            log.debug(msg);
            amqpTemplate.convertAndSend(exchange, queueKey, new Message(msg.getBytes("UTF-8"), new MessageProperties()));

        } catch (Exception e) {
            log.error("连接RabbitMQ异常", e);
        }
    }

    @Override
    public void sendPay(String out_trade_no, String paymentId, String transaction_id,
                        String dateTime, String fee) {
        try {
            PayData payData = new PayData(out_trade_no, transaction_id, paymentId, dateTime, 0, fee);
            amqpTemplate.convertAndSend(
                    GatewayProtocol.EXCHANGE_PAY_PUSH,
                    RabbitMQData.payqueue, new Message(JSON.toJSONString(payData).getBytes("UTF-8"), new MessageProperties()));

        } catch (Exception e) {
            log.error("连接RabbitMQ异常", e);
        }
    }

    @Override
    public void sendWithTtl(String exchange, String queueKey, String ttl, String msg) {
        try {
            log.debug(msg);
            MessageProperties messageProperties = new MessageProperties();
            messageProperties.setExpiration(ttl.toString());
            amqpTemplate.convertAndSend(exchange, queueKey, new Message(msg.getBytes("UTF-8"), messageProperties));

        } catch (Exception e) {
            log.error("连接RabbitMQ异常", e);
        }
    }

    @Override
    public void sendByAck(String exchange, String queueKey, String msg) {

    }


}